define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/card/index' + location.search,
                    add_url: 'user/card/add',
                    edit_url: 'user/card/edit',
                    del_url: 'user/card/del',
                    multi_url: 'user/card/multi',
                    import_url: 'user/card/import',
                    table: 'user_card',
                }
            });

            var table = $("#table");

            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                // $("input[name='store_id']", form).addClass("selectpage").data("source", "store/ajaxgetstorewithquanoption").data("primaryKey", "value").data("field", "name");
                // Form.events.cxselect(form);
                // Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'user.username', title: __('Username'), operate: 'LIKE'},
                        {field: 'user.nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'price', title: __('Price'), operate: 'between'},
                        {field: 'service_start', title: __('Service_start'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'service_end', title: __('Service_end'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'display_store_string', title: __('Store_ids'), operate: false},
                        {field: 'total_hour', title: '卡时长', operate: 'between'},
                        {field: 'hasconsumedhour', title: '已经消费时长', operate: 'between'},
                        {field: 'pay_way', title: __('Pay_way'), searchList: {"1":__('Pay_way 1'),"2":__('Pay_way 2')}, formatter: Table.api.formatter.normal},
                        // {field: 'dzmt', title: __('Dzmt'), visible: false, operate: 'LIKE'},
                        {field: 'dzmt_code', title: __('Dzmt_code'), operate: 'like'},
                        {field: 'transaction_id', title: __('Transaction_id')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'wz_button',
                                    text: '卡券使用情况',
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    icon: 'fa fa-check-square',
                                    url: function (row) {
                                        return Fast.api.fixurl('card/consumehistory?id=' + row.id)
                                    },
                                    extend: "data-area='[\"60%\", \"60%\"]'",
                                },
                            ],
                            formatter: Table.api.formatter.operate}
                    ]
                ],
                searchFormVisible: true,
                search: false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $("#c-store_ids").data("eOpen", function(){
                $('.sp_result_area').attr('style','width:500px')  
            });
            Controller.api.bindevent();
        },
        edit: function () {
            $("#c-store_ids").data("eOpen", function(){
                $('.sp_result_area').attr('style','width:500px')  
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
        }
    };
    return Controller;
});
